package com.openroom.ui.component;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.pivot.beans.BXML;
import org.apache.pivot.collections.Map;
import org.apache.pivot.collections.Sequence;
import org.apache.pivot.collections.Sequence.Tree.Path;
import org.apache.pivot.util.Resources;
import org.apache.pivot.util.Vote;
import org.apache.pivot.util.concurrent.TaskExecutionException;
import org.apache.pivot.wtk.TabPane;
import org.apache.pivot.wtk.TabPaneSelectionListener;
import org.apache.pivot.wtk.TreeView;
import org.apache.pivot.wtk.TreeViewSelectionListener;
import org.apache.pivot.wtk.content.TreeNode;
import org.apache.pivot.wtk.media.Image;

import com.openroom.event.ApplicationEvent;
import com.openroom.event.EventHandler;
import com.openroom.event.EventManager;
import com.openroom.event.EventType;
import com.openroom.model.Folder;
import com.openroom.model.ModelLocator;
import com.openroom.model.Photograph;
import com.openroom.ui.MainWindow;
import com.openroom.ui.NamespaceHelper;
import com.openroom.ui.OPMKeyHandler;
import com.openroom.ui.UIComponent;

public class FolderTreeView extends TreeView implements UIComponent {

    final URL nodeResource = MainWindow.class.getClassLoader().getResource(
            "com/openroom/ui/folder.png");
    final URL resource = MainWindow.class.getClassLoader().getResource(
            "com/openroom/ui/page_white.png");

    private static boolean initializationComplete = false;
    @BXML
    TabPane leftTabPane;
    @BXML
    TabPane previewPane;

    @Override
    public void initialize(Map<String, Object> namespace, URL arg1, Resources arg2, MainWindow window) {

        NamespaceHelper.initialize(namespace, this);

        EventManager.getInstance().listenToEventType(EventType.ALL_FOLDER_CHANGED, new EventHandler() {
            @Override
            public void handle(ApplicationEvent event) {
                addFolderToTree();
            }
        });

        EventManager.getInstance().listenToEventType(EventType.INITIALIZATION_COMPLETE, new EventHandler() {
            @Override
            public void handle(ApplicationEvent event) {
                initializationComplete = true;
                addFolderToTree();
                leftTabPane.setSelectedIndex(1);
                if (!ModelLocator.getInstance().getAllFolders().isEmpty())
                {
                    setSelectedPath(new Path(0));
                } else {
                    ModelLocator.getInstance().setAllPhotographsOfFolder(new ArrayList<Photograph>());
                }
            }
        });
        leftTabPane.getTabPaneSelectionListeners().add(new TabPaneSelectionListener() {

            @Override
            public Vote previewSelectedIndexChange(TabPane tabPane, int selectedIndex) {
                return Vote.APPROVE;
            }

            @Override
            public void selectedIndexChangeVetoed(TabPane tabPane, Vote reason) {

            }

            @Override
            public void selectedIndexChanged(TabPane tabPane, int previousSelectedIndex) {
                previewPane.setSelectedIndex(0);
            }

        });
        getTreeViewSelectionListeners().add(new TreeViewSelectionListener() {

            @Override
            public void selectedNodeChanged(TreeView arg0, Object arg1) {

            }

            @Override
            public void selectedPathAdded(TreeView arg0, Path arg1) {

            }

            @Override
            public void selectedPathRemoved(TreeView arg0, Path arg1) {

            }

            @Override
            public void selectedPathsChanged(TreeView treeView, Sequence<Path> arg1) {
                TreeNode selectedNode = (TreeNode) treeView.getSelectedNode();
                if (selectedNode != null) {
                    Folder folder = (Folder) selectedNode.getUserData();
                    ModelLocator.getInstance().setCurrentlySelectedFolder(folder);
                }
            }

        });
        getComponentKeyListeners().add(new OPMKeyHandler());
    }

    private void addFolderToTree() {
        List<Folder> allFolders = ModelLocator.getInstance().getAllFolders();
        if (initializationComplete)
        {
            org.apache.pivot.collections.List<Object> treeData = (org.apache.pivot.collections.List<Object>) getTreeData();
            treeData.clear();
            List<Folder> allFolders2 = ModelLocator.getInstance().getAllFolders();
            for (Folder folder : allFolders2) {
                try {
                    TreeNode treeNode = new TreeNode(Image.load(nodeResource), folder.getFolderName());
                    treeNode.setUserData(folder);
                    treeData.add(treeNode);
                } catch (TaskExecutionException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
